<?php

namespace pc\controllers;
use common\Service\RedisService;
use Yii;
use pc\controllers\BaseController;
use pc\models\Portray;
use pc\models\Star;
use pc\models\Tags;


class XiezhenController extends BaseController
{
    /**
     * 明星链接 /meinv/taiwan-qincun-qizi-xushen-Gnai-wanghong/page-2.html
     * 明星详情 /meinv/22.html
     *
     * 写真链接 /xiezhen/qincun/page-2.html 这里只有一个标签
     * 写真详情 /xiezhen/22.html
     */
    public function actionIndex()
    {
        $tags = trim(Yii::$app->request->get('tags'));
        $page = (int)Yii::$app->request->get('page') ?: 1;

        $tagModel = new Tags();
        $tagInfoList = $tagModel->parseTags($tags); //获取标签详情 同时干掉无效标签
        $portrayTagMenu = $tagModel->portrayTagMenu($tagInfoList); //写真标签列表


        //获取写真列表数据
        $needFields = ['id','title'];
        $result = Portray::getPortrayByTags($tagInfoList,$page,20,$needFields);


        //设置分页数据
        $pagination = $result['pagination'];
        $tagAlias = $tagInfoList ? implode('-',array_column($tagInfoList,'alias')) : '';
        if(empty($tagAlias)){
            $pagination['formatUrl'] = '/xiezhen/page-{page}.html';
            $pagination['firstUrl'] = '/xiezhen/';
        }else{
            $pagination['formatUrl'] = '/xiezhen/'.$tagAlias.'/page-{page}.html';
            $pagination['firstUrl'] = '/xiezhen/'.$tagAlias.'/';
        }
        $pageString = $this->createPageString($pagination);

        //tdk
        $tagString = empty($tagInfoList) ? '' : implode('',array_column($tagInfoList,'name'));

        return $this->render('index',[
            'all' => empty($tagInfoList) ? true : false,
            'menu' => $portrayTagMenu,
            'portrays' => $result['data'],
            'pageString' => $pageString,
            'page' => $page,
            'tagString' => $tagString
        ]);
    }

    //写真详情 相关明星【‘头像’，名字，流量，描述，星座】 你可能感兴趣的美女写真 最近更新的热门美女写真
    public function actionInfo(){
        $portrayId = (int)Yii::$app->request->get('id');
        $currentPage = (int)Yii::$app->request->get('page') ?: 1;

        $portrayInfo = Portray::getPortrayById($portrayId);
        //内容分页
        $imageNum = intval($portrayInfo['image_num']);
        $totalPage = Portray::portrayContentTotalPage($imageNum);
//        $portrayInfo['images'] = Portray::parseImages($portrayInfo['star_id'],$portrayInfo['images'],$totalPage,$currentPage);

        if($totalPage > 1){
            $portrayInfo['images'] = array_slice($portrayInfo['images'],($currentPage - 1) * 5,5);
        }


        if(empty($portrayInfo) || $currentPage > $totalPage){
            //TODO 处理404
        }
        //相关明星
        $relationStars = Star::getStarsByIds($portrayInfo['relation_star'],['id','name','nick_name','header','desc','star_sign','view']);


        //推荐写真数据
        $recommendPortrays = Portray::recommendPortrays(5);

        //随机写真数据
        $randomPortrays = Portray::randomPortrays(5,['id','title'],array_merge(array_column($recommendPortrays,'id'),['id' => $portrayId]));

//        print_r($randomPortrays);exit;

        //设置分页数据
        $pagination = [];
        $pagination['currentPage'] = $currentPage;
        $pagination['perPage'] = $imageNum > 7 ? 5 : $imageNum;
        $pagination['totalCount'] = $imageNum;
        $pagination['formatUrl'] = '/xiezhen/'.$portrayId.'-{page}.html';
        $pagination['firstUrl'] = '/xiezhen/'.$portrayId.'.html';
        $pageString = $this->createPageString($pagination);


        return $this->render('info',[
            'info' => $portrayInfo,
            'stars' => $relationStars,
            'tags' => $portrayInfo['tags'],
            'pageString' => $pageString,
            'recommendPortrays' => $recommendPortrays,
            'randomPortrays' => $randomPortrays
        ]);
    }



    public function actionSetRedis(){
//        $starModel = new Star();
//        $starModel->setStarNameToIdHash();
////        exit;
//        $model = new Portray();
//        $model->setRedis(); //设置标签hash 总列表
//        $model->insertTagToMysql(); //提取标签入库
//        $model->setStarTagRelation();
//        $tagModel = new Tags();
//        $tagModel->setRedis();
        exit;
    }

    /*
     * 获取写真图片的尺寸
     */
    public function actionImageSize(){
        $redis = Yii::$app->get('redis');

        $minPortrayId = (int)$redis->get('minPortrayId') ?: 666;
        echo $minPortrayId;
        for($i = $minPortrayId ; $i < $minPortrayId + 500 ; $i++){
            $portrayInfo = RedisService::getHash('portray:'.$i,['star_id','image_num','images']);
            if(!$portrayInfo) continue;

            $images = $portrayInfo['images'];
            if($images !== NULL){
                $images = json_decode($images,true);
                $rightImages = $newImages = [];
                if(is_array($images) && !empty($images)){
                    foreach($images as $imageAddress){
                        if(file_exists($imageAddress)){
                            $rightImageAddress = $imageAddress;
                            $imageAddress = Yii::getAlias("@storage").'/start/'.$portrayInfo['star_id'].'/'.$imageAddress;
                            $imgInfo = @getimagesize($imageAddress);
                            $image = [];
                            if(is_array($imgInfo) && !empty($imgInfo)){
                                $image['url'] = $imageAddress;
                                $image['width'] = $imgInfo[0];
                                $image['height'] = $imgInfo[1];
                                $image['ratio'] = round($image['width'] / $image['height'],3);
                                $rightImages[] = $image;
                                $newImages[] = $rightImageAddress;
                            }
                        }
                    }
                }
                $hash['images_info'] = json_encode($rightImages);
                $hash['images'] = json_encode($newImages);
                $hash['image_num'] = count($newImages);
                RedisService::setHash('portray:'.$i,$hash);
                $redis->set('minPortrayId',$i);
//                echo "<script>window.location.reload();</script>";
            }
        }
    }


    public function actionXy(){
        $redis = Yii::$app->get('redis');
        $minPortrayId = (int)$redis->get('minPortrayId') ?: 666;
        echo $minPortrayId;
        for($i = $minPortrayId; $i < $minPortrayId + 100;$i++){
            Portray::setXYImage($i);
            $redis->set('minPortrayId',$i);
        }
        if($minPortrayId < 9634){
            echo "<script>window.location.reload();</script>";
        }
    }

}
